class_name GameData
extends RefCounted

# 游戏全局数据管理
enum CharacterType {
	XU,    # 旭 - 男人，力量大
	LING,  # 玲 - 女人，速度快  
	CHEN   # 澈 - 小孩，可成长
}

enum CardType {
	ATTACK,    # 攻击卡
	SKILL,     # 技能卡
	POWER,     # 能力卡
	STATUS     # 状态卡
}

enum RelicType {
	SKATEBOARD,  # 滑板车
	CAR,         # 汽车
	TSHIRT,      # T恤
	PHONE,       # 手机
	# ... 其他11个遗物
}

enum PotionType {
	COLD_MEDICINE,  # 777感冒灵
	SUGAR_WATER,    # 糖水
	COLA,           # 可乐
	BANANA,         # 香蕉
	WINE            # 酒
}

# 游戏状态
var current_floor: int = 1
var max_floor: int = 15
var current_character: CharacterType = CharacterType.XU
var player_health: int = 100
var player_max_health: int = 100
var player_energy: int = 3
var player_max_energy: int = 3
var player_gold: int = 0

# 卡牌相关
var deck: Array[Card] = []
var hand: Array[Card] = []
var discard_pile: Array[Card] = []
var exhaust_pile: Array[Card] = []

# 遗物和药水
var relics: Array[Relic] = []
var potions: Array[Potion] = []

# 战斗状态
var in_battle: bool = false
var current_enemies: Array[Enemy] = []

# 单例模式
static var instance: GameData

func _init():
	if instance == null:
		instance = self
	else:
		push_error("GameData already exists!")

static func get_instance() -> GameData:
	if instance == null:
		instance = GameData.new()
	return instance

# 重置游戏数据
func reset_game():
	current_floor = 1
	player_health = player_max_health
	player_energy = player_max_energy
	player_gold = 0
	deck.clear()
	hand.clear()
	discard_pile.clear()
	exhaust_pile.clear()
	relics.clear()
	potions.clear()
	in_battle = false
	current_enemies.clear()

# 进入下一层
func next_floor():
	current_floor += 1
	player_energy = player_max_energy
	# 抽牌到满手牌
	draw_cards_to_hand()

# 抽牌到手牌
func draw_cards_to_hand():
	var target_hand_size = 5  # 手牌上限为5张
	var cards_drawn = 0
	
	print("开始抽牌 - 当前手牌: ", hand.size(), " 牌库: ", deck.size(), " 弃牌堆: ", discard_pile.size())
	
	# 抽牌到目标手牌数量
	while hand.size() < target_hand_size and deck.size() > 0:
		var card = deck.pop_front()
		hand.append(card)
		cards_drawn += 1
	
	# 如果牌库空了，洗牌弃牌堆
	if deck.size() == 0 and discard_pile.size() > 0:
		print("牌库空了，洗牌弃牌堆")
		shuffle_discard_into_deck()
		# 继续抽牌
		while hand.size() < target_hand_size and deck.size() > 0:
			var card = deck.pop_front()
			hand.append(card)
			cards_drawn += 1
	
	print("抽牌完成 - 抽了 ", cards_drawn, " 张牌，当前手牌: ", hand.size())

# 洗牌弃牌堆到牌库
func shuffle_discard_into_deck():
	deck = discard_pile.duplicate()
	discard_pile.clear()
	deck.shuffle()

# 使用卡牌
func use_card(card: Card):
	if card.cost > player_energy:
		return false
	
	player_energy -= card.cost
	hand.erase(card)
	discard_pile.append(card)
	return true

# 添加遗物
func add_relic(relic: Relic):
	relics.append(relic)
	relic.on_obtain()

# 使用药水
func use_potion(potion: Potion):
	potion.on_use()
	potions.erase(potion)
